//
//  DVPieChart.m
//  DVPieChart
//
//  Created by SmithDavid on 2018/2/26.
//  Copyright © 2018年 SmithDavid. All rights reserved.
//

#import "DVPieChart.h"
#import "DVFoodPieModel.h"
#import "DVPieCenterView.h"

#define CHART_MARGIN 50

@interface DVPieChart ()

@property (nonatomic, strong) DVPieCenterView *centerView;
@property (nonatomic, strong) NSMutableArray *pathArray;

@end

@implementation DVPieChart

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.pathArray = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)draw {
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self setNeedsDisplay];
}

-(void)drawRect:(CGRect)rect{
    CGFloat min = self.bounds.size.width > self.bounds.size.height ? self.bounds.size.height : self.bounds.size.width;
    CGPoint center =  CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);
    CGFloat radius = min * 0.5 - CHART_MARGIN;
    CGFloat start = - M_PI / 2;
    CGFloat angle = 0;
    CGFloat end = start;
    CGFloat totalPercent = 0;
    
    if (self.dataArray.count == 0) {
        end = start + M_PI * 2;
        UIColor *color = kAppNormalTextColor;
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:start endAngle:end clockwise:true];
        [color set];
        
        //添加一根线到圆心
        [path addLineToPoint:center];
        [path fill];
        [self.pathArray addObject:path];
    } else {
        for (int i = 0; i < self.dataArray.count; i++) {
            DVFoodPieModel *model = self.dataArray[i];
            CGFloat percent = model.rate;
            totalPercent += percent;
            CGFloat minusValue = totalPercent - self.battlePercent;
            UIColor *color;
            if (minusValue < 0.0417) {
                color = [UIColor colorWithHexString:@"e50113"];
            } else {
                color = kAppNormalTextColor;;
            }
            
            start = end;
            angle = percent * M_PI * 2;
            end = start + angle;

            UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:(start + 0.002) endAngle:(end - 0.002) clockwise:true];
            
            [color set];
            //添加一根线到圆心
            [path addLineToPoint:center];
            [path fill];
        }
    }
    
    // 在中心添加label
    self.centerView = [[DVPieCenterView alloc] init];
    self.centerView.frame = CGRectMake(0, 0, 150, 150);
    
    CGRect frame = self.centerView.frame;
    frame.origin = CGPointMake(self.frame.size.width * 0.5 - frame.size.width * 0.5, self.frame.size.height * 0.5 - frame.size.width * 0.5);
    self.centerView.frame = frame;
    self.centerView.nameLabel.text = self.title;
    [self addSubview:self.centerView];
}

- (void)setBattlePercent:(CGFloat)battlePercent
{
    _battlePercent = battlePercent;
    self.title = [NSString stringWithFormat:@"%.0f%@", battlePercent * 100, @"%"];
    [self draw];
}
@end

